package com.example.zyghfeedback;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.constraintlayout.solver.ArrayLinkedVariables;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static android.net.sip.SipErrorCode.SERVER_ERROR;

public class MyDetailsAdapter extends RecyclerView.Adapter {
    private Bitmap bitmap;
//    public static final int GET_DATA_SUCCESS = 1;
//    public static final int NETWORK_ERROR = 2;
//    public static final int SERVER_ERROR = 3;


    private List<MyFeedbackDetailsModel> myFeedbackDetailsModels;
    //子线程不能操作UI，通过Handler设置图片
//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what){
//                case GET_DATA_SUCCESS:
//                    Bitmap bitmap = (Bitmap) msg.obj;
//
////                    setImageBitmap(bitmap);
//                    break;
//                case NETWORK_ERROR:
////                    Toast.makeText(getContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
//                    break;
//                case SERVER_ERROR:
////                    Toast.makeText(getContext(),"服务器发生错误",Toast.LENGTH_SHORT).show();
//                    break;
//            }
//        }
//    };

    //设置常量
    //平台
    private static final int LEFT = 1;
    //用户
    private static final int RIGHT = 0;
    //头部
    private static final int TITLE = 3;
    private LayoutInflater inflater;
    private Context mContext;

    public MyDetailsAdapter(List<MyFeedbackDetailsModel> myFeedbackDetailsModels, Context context) {
        this.myFeedbackDetailsModels = myFeedbackDetailsModels;
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder holder = getViewHolderByViewType(viewType);
        return holder;
    }

    /**
     * 根据不同的position，设置不同的ViewType
     * position表示当前是第几个Item，通过position拿到当前的Item对象，然后判断这个item对象需要那种视图
     */
    @Override
    public int getItemViewType(int position) {
        if (myFeedbackDetailsModels.get(position).getSource().equals("1")) {
            return LEFT;
        } else if (myFeedbackDetailsModels.get(position).getSource().equals("0")) {
            return RIGHT;
        } else if (myFeedbackDetailsModels.get(position).getSource().equals("null")) {
            return RIGHT;
        } else if (myFeedbackDetailsModels.get(position).getSource().equals("3")) {
            return TITLE;
        }
        return 0;
    }

    private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {
        View left = View.inflate(mContext, R.layout.left_item, null);
        View right = View.inflate(mContext, R.layout.right_item, null);
        View title = View.inflate(mContext, R.layout.title_item, null);
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case LEFT:
                holder = new ViewHolderLeft(left);
                break;
            case RIGHT:
                holder = new ViewHolderRight(right);
                break;
            case TITLE:
                holder = new ViewHolderTitle(title);
                break;
        }
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // 绑定数据
        MyFeedbackDetailsModel myFeedbackModel = myFeedbackDetailsModels.get(position);
        if (holder instanceof ViewHolderLeft) {
            ((ViewHolderLeft) holder).tv_text.setText(myFeedbackModel.getMessage());
            ((ViewHolderLeft) holder).tv_date.setText(getUserDate(myFeedbackModel.getCreateTime()));
        } else if (holder instanceof ViewHolderRight) {
            if (myFeedbackModel.getType().equals("1")) {
                ((ViewHolderRight) holder).tv_text.setVisibility(View.GONE);
                ((ViewHolderRight) holder).iv_photo.setVisibility(View.VISIBLE);
                ((ViewHolderRight) holder).iv_photo.setImageBitmap(setImageURL(myFeedbackModel.getMessage()));
            } else {
                ((ViewHolderRight) holder).tv_text.setVisibility(View.VISIBLE);
                ((ViewHolderRight) holder).iv_photo.setVisibility(View.GONE);
                ((ViewHolderRight) holder).tv_text.setText(myFeedbackModel.getMessage());
                ((ViewHolderRight) holder).tv_date.setText(getUserDate(myFeedbackModel.getCreateTime()));
            }

        } else if (holder instanceof ViewHolderTitle) {
            ((ViewHolderTitle) holder).tv_text.setText(myFeedbackModel.getMessage());
            ((ViewHolderTitle) holder).tv_date.setText(getUserDate(myFeedbackModel.getCreateTime()));
        }
    }

    public static String getUserDate(long sformat) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = formatter.format(new Date(sformat));   // 时间戳转换成时间     (这里也是毫秒时间戳)
        return sd;
    }

    @Override
    public int getItemCount() {
        return myFeedbackDetailsModels == null ? 0 : myFeedbackDetailsModels.size();
    }

    private class ViewHolderLeft extends RecyclerView.ViewHolder {
        TextView tv_date, tv_text;
        ImageView iv_head;

        public ViewHolderLeft(View itemView) {
            super(itemView);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_text = itemView.findViewById(R.id.tv_text);
            iv_head = itemView.findViewById(R.id.iv_head);
        }
    }
    //设置网络图片
    public Bitmap setImageURL(final String path) {
        //开启一个线程用于联网
        new Thread() {
            @Override
            public void run() {
                try {
                    //把传过来的路径转成URL
                    URL url = new URL(path);
                    //获取连接
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //使用GET方法访问网络
                    connection.setRequestMethod("GET");
                    //超时时间为10秒
                    connection.setConnectTimeout(5000);
                    //获取返回码
                    int code = connection.getResponseCode();
                    if (code == 200) {
                        InputStream inputStream = connection.getInputStream();
                        //使用工厂把网络的输入流生产Bitmap
                        bitmap = BitmapFactory.decodeStream(inputStream);
//                        //利用Message把图片发给Handler
//                        Message msg = Message.obtain();
//                        msg.obj = bitmap;
//                        msg.what = GET_DATA_SUCCESS;
//                        handler.sendMessage(msg);
//                        inputStream.close();
                    }
//                    else {
////                        //服务启发生错误
////                        handler.sendEmptyMessage(SERVER_ERROR);
//                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    //网络连接错误
//                    handler.sendEmptyMessage(NETWORK_ERROR);
                }
            }
        }.start();
        return bitmap;
    }

    private class ViewHolderRight extends RecyclerView.ViewHolder {
        TextView tv_date, tv_text;
        ImageView iv_head;
        ImageView iv_photo;

        public ViewHolderRight(View itemView) {
            super(itemView);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_text = itemView.findViewById(R.id.tv_text);
            iv_head = itemView.findViewById(R.id.iv_head);
            iv_photo = itemView.findViewById(R.id.iv_photo);
        }
    }

    private class ViewHolderTitle extends RecyclerView.ViewHolder {
        TextView tv_date, tv_text;
        ImageView iv_head;

        public ViewHolderTitle(View itemView) {
            super(itemView);
            tv_date = itemView.findViewById(R.id.tv_date);
            tv_text = itemView.findViewById(R.id.tv_text);
            iv_head = itemView.findViewById(R.id.iv_head);
        }
    }
}
